<template>
    <div>
        <div id="background"></div>
        <div>
            <el-card>
                <img src="../assets/photo/login/ico.png" alt="寄" id="logo">
                <div id="title">大模型编程助手</div>
                <div id="input_frame"> 
                    <el-input v-model="username" placeholder="请输入账号" id="username" class="underline-input"></el-input>
                    <el-input placeholder="请输入密码" v-model="password" show-password class="underline-input"></el-input>
                    <div type="primary" plain v-on:click="signup" id="signup">注册</div>
                    <el-button type="primary" v-on:click="log" id="log">登录</el-button>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script>
import { Message } from 'element-ui';
import { login } from '../api/login';
export default {
    data() {
        return {
            username: '',
            password: ''
        };
    },
    methods: {
        async log(){
            if (this.password === '') {
                Message({
                    message: '请输入密码',
                    type: 'warning'
                });
                return;
            }
            // 直接调用api尝试从数据库中查找账户---->没有了内存数据库，改为调用api
            const getData = await login(this.$data.username, this.$data.password); 
            if (getData.status === "200"){
                // console.log(getData.data);
                
                const account = getData.data.account;
                const name = getData.data.name;
                const userid = getData.data.user_id;
                const userInfo = {
                    accountName: name,
                    user_id: userid
                };
                localStorage.setItem(account,JSON.stringify(userInfo));  // 本地缓存（账号-->用户名，用户id）--->拿不到密码了
                Message({
                    message: '登录成功',
                    type: 'success'
                });
                localStorage.setItem('isLoggedIn', 'true');
                this.$isLoggedIn = true; // 更新 Vue 实例的登录状态
                localStorage.setItem("auto_log_people",this.username);  // 更改自动登录的账号
                localStorage.setItem('loginTime', new Date().getTime()+86400000);  // 自动登录时限（1天） 
                this.$router.push({ name: 'main'});
                return;
            }else{
                Message({
                    message: "账号或密码错误",
                    type: 'error'
                });
                this.username = '';
                this.password = '';
                return;
            }
        },
        signup() {
            this.$router.push('/signup');
        }
    }
};
</script>

<style>
    html, body, #app {
        margin: 0;
        padding: 0;
        height: 100%;
        width: 100%;
    }

    #background {
        position: fixed;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        background-image: url("../assets/photo/login/background.jpg");
        background-repeat: no-repeat;
        background-size: cover;
    }

    .el-card {
        width: 30vw;
        height: 35vw;
        max-width: 440px !important;
        position: absolute;
        top: 48%;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: rgba(255, 255, 255, 0.5);
        border-radius: 15px !important;
        padding: 20px;
        text-align: center;
    }

    #logo {
        width: 50%;
        height: auto;
        margin-bottom: 20px;
    }

    #title {
        font-size: 2.5vw;
        text-align: center;
        margin-bottom: 2vw;
        margin-top: -60px;
    }

    #input_frame {
        margin-top: 2vw;
    }

    #username {
        margin-bottom: 2vw;
    }
    
    #signup {
        margin-top: .5vw;
        font-size: 1vw;
        margin-bottom: 2vw;
        color: #555;
        cursor: pointer;
        float: right;
    }

    #log {
        width: 60%;
        margin-top: 4vw;
        margin-left: 2.5vw !important;
    }

    .underline-input .el-input__inner {
        border: none;
        border-bottom: 1px solid #dcdfe6;
        border-radius: 0;
        box-shadow: none;
    }

    .underline-input .el-input__inner:focus {
        border-bottom-color: #409eff;
        box-shadow: none;
    }

    .underline-input .el-input__inner:hover {
        border-bottom-color: #c0c4cc;
    }

    @media (max-width: 768px) {
        .underline-input .el-input__inner{
            font-size: 12px;
        }
        .el-card {
            width: 60vw;
            height: auto;
            padding: 10px;
        }

        #title {
            font-size: 6vw;
            margin-bottom: 5vw;
            margin-top: -13vw;
        }

        #logo {
            width: 80%;
            margin-top: -5vw;
        }

        #input_frame {
            margin-top: 5vw;
        }

        el-input,
        el-button {
            width: 100%;
        }

        #username {
            margin-bottom: 2vw;
        }
        #signup {
            margin-top: 1vw;
            font-size: 3.3vw;
            margin-bottom: 2vw;
        }

        #log {
            width: 100%;
            margin-top: 0;
        }
    }
</style>

